package com.classeven.downhill.element;

import org.andengine.entity.primitive.Rectangle;

import com.classeven.downhill.engine.Anchor;
import com.classeven.downhill.engine.GameEngine;
import com.classeven.downhill.engine.Utils;
import com.classeven.downhill.game.element.GameElementRectangle;

public class ARectangle 
{
	GameEngine theEngine;
	Rectangle rect;
	
	public ARectangle(GameEngine mainActivity, GameElementRectangle[] arrayRect, int index)
	{
		theEngine = mainActivity;
		Load(arrayRect, index);
	}
	
	public void Load(GameElementRectangle[] arrayRect, int index)
	{
		if(arrayRect[index].isAutoResize())
		{
			rect = new Rectangle( 0, 0, Utils.GetRatioWidth(arrayRect[index].getW()), Utils.GetRatioHeight(arrayRect[index].getH()), theEngine.getVertexBufferObjectManager());
			SetPos(arrayRect[index].getX(), arrayRect[index].getY(), arrayRect[index].getAnchor());
		}
		else
		{
			rect = new Rectangle( 0, 0, arrayRect[index].getW(), arrayRect[index].getH(), theEngine.getVertexBufferObjectManager());
			rect.setPosition(Utils.GetAnchorX(rect.getWidth(), arrayRect[index].getAnchor()) + arrayRect[index].getX(), 
					Utils.GetAnchorY(rect.getHeight(), arrayRect[index].getAnchor()) + arrayRect[index].getY());
		}
		
		rect.setColor(arrayRect[index].getR(), arrayRect[index].getG(), arrayRect[index].getB());
	}
	
	public void SetPos(float x, float y, Anchor anchor)
	{
		float _x, _y;
		_x = Utils.GetAnchorX(rect.getWidth(), anchor) + Utils.GetRatioWidth(x);
		_y = Utils.GetAnchorY(rect.getHeight(), anchor) + Utils.GetRatioHeight(y);
		
		rect.setPosition(_x, _y);
	}
	
	public Rectangle theRect()
	{
		return rect;
	}
	
	public void AddText(AText text)
	{
		rect.attachChild(text.theText());
		int x, y;
		x = (int) (rect.getWidth()/2 - text.theText().getWidth()/2);
		y = (int) (rect.getHeight()/2 - text.theText().getHeight()/2);
		text.theText().setPosition(x, y);
	}
}
